using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Advanced_Terrain
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class CullingClass : Microsoft.Xna.Framework.GameComponent
    {
        BoundingFrustum Frustum;
        public CullingClass(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            Frustum = new BoundingFrustum(CameraProperties.View * CameraProperties.projectionMatrix);
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            int index = 0;
            if (Game1.newTerrain.TreeClassList != null)
            {
                Frustum.Matrix = CameraProperties.View * CameraProperties.projectionMatrix;
                foreach (TreeClass Trees in Game1.newTerrain.TreeClassList)
                {
                    if (Frustum.Intersects(Trees.Box))
                    {
                        Game1.newTerrain.TreeClassList[index].Visible = true;
                        
                    }
                    else
                    {
                        Game1.newTerrain.TreeClassList[index].Visible = false;
                        
                    }


                }
                index++;
            }
            base.Update(gameTime);
        }
    }
}